<template>
<!--投资方案-->
  <div>
      <public-table
              ref="publicTableRef"
              index
              :selection="false"
              :indexWidth="'100'"
              :header-cell-style="{'background':'#f7f7fa'}"
              border
              :page="page"
              :tableData="tableData"
              :tableOption.sync="tableOption"
              @page-change="onPageChange"
      >
          <template #buttons="scope">
              <el-button class="button_view" @click="download(scope.row)">附件下载</el-button>
          </template>
      </public-table>
  </div>
  <div class="button-wrap d-flex a-center j-center m-t20">
      <el-button class="button_cancel" @click="goBack">返回</el-button>
  </div>

  <public-dialog
          :title="'附件下载'"
          :width="'60%'"
          v-model:visible="visible"
          @onConfirm="onConfirm"
          :footer-button="false">
      <public-table
              ref="publicTableRef"
              index
              :is-paging="false"
              :selection="false"
              :indexWidth="'100'"
              :header-cell-style="{'background':'#f7f7fa'}"
              border
              :page="page"
              :tableData="fileList"
              :tableOption.sync="tableOption2">
          <template #buttons="scope">
              <el-button class="button_view" @click="fileDownload(scope.row)">附件下载</el-button>
          </template>
      </public-table>
  </public-dialog>
</template>

<script setup>
import {useRouter} from "vue-router";
import {reactive, ref} from "vue";
import PublicTable from "@/components/PublicTable.vue";
import {tableCommonFun} from "@/hooks/tableCommon";
import PublicDialog from "@/components/PublicDialog.vue";
import {reserveInvestmentProjectLibraryApi} from "@/api/investment";
import {exportFile} from "@/common/utils/GlobalUtils";
import {ElMessage} from "element-plus";

const interfaceConfig = reactive({
    fileName:'investment',
    apiPath:'reserveInvestmentProjectLibraryApi.getInvestmentPlanPage',
});


const router = useRouter();
const projectId  = router.currentRoute.value.params.projectId;

/**
 * 默认参数
 * @type {UnwrapNestedRefs<{prepareStatus: number}>}
 */
const defaultParameters = reactive({
    projectId:projectId
});

const {
    form,
    page,
    tableOption,
    tableData,
    onQuery,
    onReset,
    onPageChange,
    getData,
    onExport
}  = tableCommonFun(interfaceConfig,{},defaultParameters);

tableOption.value = [
  { label: "方案名称", prop: "planName", minWidth: 120 },
  { label: "会议议题", prop: "meetingTopics", minWidth: 120 },
  { label: "参会人", prop: "participants", minWidth: 100 },
  { label: "会议记录", prop: "meetingMinutes", minWidth: 100 },
  {
    label: "审批结果", prop: "status", formatter: (val, row) => {
      return ['', '审批中', '已通过', '已驳回'][val] || '-';
    }, minWidth: 100
  },
  { label: "审批意见", prop: "approvalOpinions", minWidth: 100 },
  { label: "审批时间", prop: "approvalTime", minWidth: 150 },
  { label: "审批人", prop: "userName", minWidth: 100 },
  {
    label: '操作',
    slot: true,
    width: 200,
    overHidden: false,
    slotName: 'buttons',
      fixed: "right"
  },
];

const fileList = ref([]);
const tableOption2 =  ref([
    { label: "附件名称", prop: "fileName"},
    {
        label: '操作',
        slot: true,
        width: 150,
        overHidden: false,
        slotName: 'buttons',
        fixed: "right"
    },
]);
const visible = ref(false);
const onConfirm = () => {
    visible.value = false;
};

/**
 * 下载
 * @param row
 */
const download = (row) => {
    fileList.value = row.files;
    visible.value = true;

    console.log('row',row);
}

const fileDownload = (row) => {
    if(row.fileUrlKey){
        reserveInvestmentProjectLibraryApi.getPlanWord({templateUrlKey:row.fileUrlKey}).then(res=>{
            if(res){
                let fileName = `投资方案`;
                let fileType = row.fileUrlKey.split('.')[row.fileUrlKey.split('.').length-1];
                let blobType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8';
                exportFile(res,fileName,fileType,blobType);
            }
        });
    }else{
        ElMessage.error('下载链接不存在,请联系管理员!');
    }
}

/**
 * 返回
 */
const goBack = () => {
    // router.push({path:`/investmentProjectManagement/investmentProjects/investmentProjectInformation/investmentProjectsList`});
    router.go(-1);
}

defineExpose({
    getData,
});
</script>

<style scoped>

</style>
